# https://zyjy.as.xm.gov.cn/transactionInfo.html?tab=%E6%94%BF%E5%BA%9C%E9%87%87%E8%B4%AD

import requests
import re

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json;charset=UTF-8',
    'Origin': 'https://zyjy.as.xm.gov.cn',
    'Pragma': 'no-cache',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}


def getInfo(projId):
    params = {
        'projId': projId,
        'locaJson': 'false',
    }

    response = requests.get(
        'https://zyjy.as.xm.gov.cn/prod-api/api/trade/purchase/getInfo',
        params=params,
        headers=headers,
    )

    for item in response.json()['data']:
        if item['detailFileObjList'] is not None:
            try:
                html = item['detailFileObjList'][0]['content']
                html = re.sub('\s', '', html).strip()
                ysje = re.findall("<p>预算金额：(.*?)元</p>", html)[0]
                print(ysje)
            except IndexError as e:
                html = item['detailFileObjList'][0]['content']
                html = re.sub('\s', '', html).strip()
                ysje = re.findall("预算金额：<span>(.*?)元</span>", html)[0]
                print(ysje)


def purchaseList():
    json_data = {
        'dataType': 'quali_inquery_ann',
        'tendermode': '',
        'pageNum': 1,
        'pageSize': 10,
        'showRange': '',
        'proName': '',
    }
    response = requests.post(
        'https://zyjy.as.xm.gov.cn/prod-api/api/trade/purchase/purchaseList',
        headers=headers,
        json=json_data,
    )
    print(response.json()['data'])
    for item in response.json()['data']:
        getInfo(item['projid'])


if __name__ == '__main__':
    purchaseList()
